# mini taeko ... test for pygame music
# Copyright (C) 2007 Sergio Perez Camacho - BRUE
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import pygame
from pygame.locals import *
import sys

WIN_X = 640
WIN_Y = 480
ER = 10
GR = 20
MR = 30
BPS= 140/60

class sound_kit:    
    def __init__(self, sound_files):
        self.kit = []
        for file in sound_files:
             self.kit.append(pygame.mixer.Sound(file))
        
                
    def play(self, index):        
        if index in range(0, len(self.kit)):
            for c in channels:
                if not c.get_busy():
                    c.play(self.kit[index])
                    break
        else:
            print "No kit number avaiable"

        
    def stop(self, index):
        if index in range(0, len(self.kit)):
            self.kit[index].stop()
        else:
            print "No kit number avaiable"

class beat:
    def __init__(self, time):
        self.time = time
    
    def draw(self, musictime,s):
#        print "-----"
#        print musictime-MR
#        print self.time
#        print musictime+1000
#        print "------"
        if self.time in range(musictime-MR,musictime+1000):
            if self.time in range(musictime-GR,musictime+GR):
                pygame.draw.circle(s, (255,255,255), (30+((self.time-musictime)/BPS),40), 20, 0)
            else:
                pygame.draw.circle(s, (100,100,100), (30+((self.time-musictime)/BPS),40), 18, 0)
    

def init_mixer(n):
    lchannels = []
    pygame.mixer.init()
    pygame.mixer.set_num_channels(n)
    for i in range(0,n):
        print i
        lchannels.append(pygame.mixer.Channel(i))
    return lchannels

if __name__ == "__main__":
    
    channels = init_mixer(40)

    sk = sound_kit(["snare.ogg","kick.ogg"])
    pygame.mixer.music.load("Access_Denied.ogg")

    
    pygame.init()
    
    screen = pygame.display.set_mode((WIN_X,WIN_Y), HWSURFACE|DOUBLEBUF)    
    
    surf = pygame.Surface(screen.get_size())
    screen.blit(surf, (0,0))
    pygame.display.flip()
    

    pygame.mixer.music.play(-1)
    
    l = open("test_l", "r")

    m = []
    
    for linea in l:
 #       print linea[:-1]
        m.append(beat(int(linea[:-1])))
    
    
    f = open("test", "w")
    
    done = False
    while not done:
        surf.fill((0,0,0))        
        for j in m:
            j.draw(pygame.mixer.music.get_pos(),surf)        
        screen.blit(surf, (0,0))
#        print "-" + str(pygame.mixer.music.get_pos())
        pygame.display.flip()
        
        for event in pygame.event.get():
            if (event.type == KEYUP):
                    pass
                    
            if (event.type == KEYDOWN):
                if (event.key==K_SPACE):
                    print "added!"
                    value = str(pygame.mixer.music.get_pos())+"\n"
                    f.write(value)
                if (event.key==K_ESCAPE): done=True
                if (event.key==K_q):
                    sk.play(0)
                    for j in m:
                        if j.time in range(pygame.mixer.music.get_pos()-MR,pygame.mixer.music.get_pos()+MR):
                            print "BUENA!"
                        
                if (event.key==K_p):
                    sk.play(1)

    f.close()
    
    pygame.mixer.quit()        
    pygame.quit()
    
